package com.example.lancooznbk.utils;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.andview.refreshview.XRefreshView;
import com.example.lancooznbk.view.widget.CustomHeader;
import com.example.lancooznbk.view.widget.EmptyMainLayout;
import com.example.lancooznbk.view.widget.EmptyViewLayout;

/**
 * Created by tangminglong on 17/7/20.
 */

public class SwipRefreshHelper {

    private static final int mPinnedTime = 1500;

    public static void init(XRefreshView xRefreshView, EmptyMainLayout emptyLayout, XRefreshView.SimpleXRefreshListener listener){
        xRefreshView.setPinnedTime(mPinnedTime);
        xRefreshView.setMoveForHorizontal(true);
        xRefreshView.setPullLoadEnable(true);//能够加载更多
        xRefreshView.setAutoLoadMore(true);//自动加载更多
        xRefreshView.enableReleaseToLoadMore(true);//recycler 松开加载更多
        xRefreshView.enableRecyclerViewPullUp(true);
        xRefreshView.enablePullUpWhenLoadCompleted(true);//加载完毕之后允许继续往上滑
        xRefreshView.setEmptyView(emptyLayout);
        xRefreshView.enableEmptyView(true);//初始化支持
        // xRefreshView.setCustomFooterView(new CustomFooterView(activity));
        xRefreshView.setCustomHeaderView(new CustomHeader(emptyLayout.getContext(),mPinnedTime));
        xRefreshView.setXRefreshViewListener(listener);
    }

    public static int[] getPositionAndOffset(RecyclerView recyclerView){
        int[] positionAndOffset = new int[2];
        LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
        //获取可视的第一个view
        View topView = layoutManager.getChildAt(0);
        if(topView != null) {
            //得到该View的数组位置
            positionAndOffset[0] = layoutManager.getPosition(topView);
            //获取与该view的顶部的偏移量
            positionAndOffset[1] = topView.getTop();
        }
        return positionAndOffset;
    }
}
